/**
 * 将actionCreator转化为dispatch方法的调用
 * @param {*} actionCreator 
 * @param {*} dispatch 
 * @returns 
 */
function bindActionCreator(actionCreator, dispatch) {
  return function(...args) {
    dispatch(actionCreator.apply(null, args));
  };
}

/**
 * 将actionCreator转化为dispatch方法的调用
 * @param {*} actionCreators 
 * @param {*} dispatch 
 * @returns 
 */
export default function bindActionCreators(actionCreators, dispatch) {
  if (typeof actionCreators === 'function') {
    return bindActionCreator(actionCreators, dispatch);
  }

  if (typeof actionCreators !== 'object' || actionCreators === null) {
    throw new Error('bindActionCreators expected an object or a function');
  }

  const boundActionCreators = {};
  // 循环actionCreators，将其转换为dispatch方法的调用
  for (let key in actionCreators) {
    boundActionCreators[key] = bindActionCreator(actionCreators[key], dispatch);
  }
  return boundActionCreators;
}